package com.cn.medicalcare.controller.backstageservlet.admin;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.Admin;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/getadmininfo.json")
public class AdminInfo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String token = req.getParameter("sessionId");
        Admin admin = null;
        Map<String, Object> map = new LinkedHashMap<>();
        if (StringUtils.isNotEmpty(token)) {
            admin = JsonTool.toObject(JedisUtil.jedisDo(j -> {
                j.select(15);
                return j.get(token);
            }), Admin.class);
        }
        if (admin != null) {
            map.put("status", 200);
            map.put("admin_user", admin);
        } else {
            map.put("status", 404);
        }
        resp.getWriter().print(JsonTool.toJson(map));
    }
}
